from rest_framework import serializers
from goods.models import SKU


class CartSerializer(serializers.ModelSerializer):
    count = serializers.IntegerField()
    selected = serializers.BooleanField()

    class Meta:
        model = SKU
        fields = ['id', 'count', 'selected', 'name', 'default_image_url', 'price']


class CartUpdateSerializer(serializers.Serializer):
    sku_id = serializers.IntegerField()
    count = serializers.IntegerField(min_value=1)
    selected = serializers.BooleanField(default=True)

    def validate_sku_id(self, value):
        if SKU.objects.filter(pk=value).count() <= 0:
            raise serializers.ValidationError('商品编号无效')
        return value
